﻿using OestsCommon.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace OestsDataBase
{
    public static class MajorDAL
    {
        //专业
        public static void CreateTable()
        {
            string sqlCheck = "if object_id( 'T_Major') is not null select 1 else select 0";
            if ((int)SqlHelper.ExecuteScalar(sqlCheck) == 0)
            {
                string sqlCreacte = @"CREATE TABLE T_Major (id bigint primary key ,
                                name NVARCHAR(MAX) not null,activate bit not null)";

                SqlHelper.ExecuteNonQuery(sqlCreacte);
            }
        }

        public static List<MajorModel> GetData()
        {
            List<MajorModel> list = new List<MajorModel>();
            string strSql = "select * from T_Major";
            DataTable dt = SqlHelper.ExecuteDataTable(strSql);
            if (dt == null) { return null; }
            foreach (DataRow dr in dt.Rows)
            {
                MajorModel s = new MajorModel();
                s.Id = Int32.Parse(dr["id"].ToString());
                s.Name = dr["name"].ToString();
                bool b = false;
                Boolean.TryParse(dr["activate"].ToString(), out b);
                s.Activate = b;
                list.Add(s);
            }
            return list;

        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool Modify(MajorModel model)
        {
            int nActivate = model.Activate ? 1 : 0;
            string strSql = string.Format("update T_Major set name='{0}',activate ={1}  where id = @id", model.Name, nActivate);
            int n = SqlHelper.ExecuteNonQuery(strSql, new SqlParameter("@id", model.Id));
            if (n != 0)
            {
                return true;
            }
            return false;
        }

    }
}
